package com.gudaomai.ekproject.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gudaomai.ekproject.common.CommonConstant;
import com.gudaomai.ekproject.common.CommonException;
import com.gudaomai.ekproject.common.GudaomaiGrantedAuthority;
import com.gudaomai.ekproject.common.MsgConstant;
import com.gudaomai.ekproject.entity.Permission;
import com.gudaomai.ekproject.entity.Role;
import com.gudaomai.ekproject.entity.Sysuser;
import com.gudaomai.ekproject.mapper.SysuserMapper;
import com.gudaomai.ekproject.service.IPermissionService;
import com.gudaomai.ekproject.service.IRoleService;
import com.gudaomai.ekproject.service.ISysuserService;
import com.gudaomai.ekproject.service.impl.BaseServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * <p>
 * sysuser 服务实现类
 * </p>
 *
 * @author zenghualu
 * @since 2017-06-26
 */
@Service
public class SysuserServiceImpl extends BaseServiceImpl<SysuserMapper, Sysuser> implements ISysuserService {

	@Autowired
	IPermissionService permissionServ;
	@Autowired
	IRoleService roleServ;
	@Autowired
	SysuserMapper sysuserMapper;

	@Override
	public UserDetails loadUserByUsername(String username) {
		// 判断用户是否存在
		Sysuser sysuser = null;
		try {
			sysuser = getUserByName(username);
		} catch (CommonException e1) {
			e1.printStackTrace();
		}
		if (sysuser == null) {
			throw new UsernameNotFoundException(username + "用户名不存在");
		}

		List<Role> roles = null;
		try {
			roles = roleServ.getRolesByUserId(sysuser.getId());
		} catch (CommonException e) {
			e.printStackTrace();
		}

		// 根据角色获取权限
		List<Permission> permissions = new ArrayList<Permission>();
		if (roles != null) {
			for (Role role : roles) {
				List<Permission> perms = permissionServ.getPermissionsByRoleId(role.getId());
				permissions.addAll(perms);
			}
		}

		// 定义权限集合
		List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();

		// 添加权限到集合中
		if (permissions != null) {
			for (Permission permission : permissions) {
				if (permission != null) {
					GrantedAuthority grantedAuthority = new GudaomaiGrantedAuthority(
							"AUTH_" + permission.getName() + "_" + permission.getOperation());
					grantedAuthorities.add(grantedAuthority);
				}
			}
		}

		// 将已登录的用户信息保存到SecurityContext
		User user = new User(sysuser.getName(), sysuser.getPassword(), true, true, true, true, grantedAuthorities);
		return user;
	}

	/**
	 * 根据用户名获取用户信息
	 * 
	 * @param username
	 * @return Sysuser
	 * @throws CommonException
	 */
	// 根据用户名获取用户
	public Sysuser getUserByName(String username) throws CommonException {

		EntityWrapper<Sysuser> ew = new EntityWrapper<>();
		ew.setEntity(new Sysuser());
		ew.eq("name", username);

		Sysuser sysuser = this.selectOne(ew);

		if (null == sysuser) {
			throw new CommonException(CommonConstant.RESULT_CODE_NOT_FOUND, MsgConstant.RET_USER_SELECT_NOTEXIST);
		} else {
			sysuser.setRetCode(CommonConstant.RESULT_CODE_OK);
			sysuser.setRetMessage(MsgConstant.RET_USER_SELECT_ALREADY_EXIST);
		}

		return sysuser;
	}// method getUserByName

	// 判断用户是否存在
	@Override
	public boolean isExist(String sysuserName) {

		EntityWrapper<Sysuser> ew = new EntityWrapper<>();
		ew.setEntity(new Sysuser());
		ew.eq("name", sysuserName);

		Sysuser sysuser = this.selectOne(ew);

		if (sysuser == null) {
			return false;
		}
		return true;
	}

}
